<?php

namespace App\Eloquent\Ygt;

use Framework\BaseClass\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class CategoryFields extends DbEloquent
{
    use SoftDeletes;
    protected $table = 'ygt_category_fields';
    protected $dates = ['deleted_at'];

    //取所有的自定义字段
    public static function getFieldListByType($categoryId,$type=0)
    {
        $where          = [ 'category_id'=>$categoryId ];
        if($type != 0)
        {
            $where['type']  = $type;
        }
        //$fields         = 'id,type,field_name,field_type,placeholder,is_must,field_value,min_value,max_value';
        $fields         = 'id,type,field_name,field_type,field_unit,placeholder,is_must';
        $limit          = $offset = '';
        $orderby        = ['sort_id','asc'];
        $colletion      = self::getList($where,$fields,$limit,$offset,$orderby);
        return $colletion;
    }
    //自定义字段分组,标准和非标区分开,不交叉
    public static function getGroupFieldListByType($categoryId,$type=0)
    {
        $colletion      = self::getFieldListByType($categoryId,$type);
        $group          = $colletion->groupBy('type')->all();
        $basicArr       = $nonstandardArr = [];
        if(isset($group[1]))
        {
            $basicArr   = $group[1];
        }
        if(isset($group[2]))
        {
            $nonstandardArr = $group[2];
        }
        $result         = [
            'basic_arr'=>$basicArr,'non_arr'=>$nonstandardArr
        ];
        return $result;
    }
    //自定义字段分组,标准和非标区分开,交叉[非标来源于标准]
    public static function getCustomFieldListByType($categoryId)
    {
        $colletion      = self::getFieldListByType($categoryId);
        $lists          = $colletion->toArray();
        $basicArr       = $nonstandardArr = [];
        foreach($lists as $key=>$val)
        {
            $basicArr[]             = $val;
            if($val['type'] == 2)
            {
                $nonstandardArr[]   = $val;
            }
        }
//        $result         = [
//            'basic_arr'=>$basicArr,'non_arr'=>$nonstandardArr
//        ];
        $result         = [
            'basic_arr'=>$basicArr,'non_arr'=>[]
        ];
        return $result;
    }
}
